﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Controls_SpendExpControl : System.Web.UI.UserControl
{
    private String guid;
    private IEnumerator<HeresyManager.Comunication.Comunicator> comunicatro;

    public HeresyManager.Character Character
    {
        get { return UserData.CurrentCharacter; }
    }
    private int idCount;



    protected override void OnInit(EventArgs e)
    {
        base.OnInit(e);
        guid = Guid.NewGuid().ToString();
    }

    protected void Page_Load(object sender, EventArgs e)
    {

        if (comunicatro != null)
        {
            this.NewRankButton.Visible = false;
            this.ChooseRankButton.Visible = true;
            ComunicationControl1.Comunicator = comunicatro.Current;
            //if (!comunicatro.MoveNext())
            //    comunicatro = null;
            return;
        }
        else
        {
            this.NewRankButton.Visible = true;
            this.ChooseRankButton.Visible = false;

            ComunicationControl1.Comunicator = null;


            CreateControl();
        }
    }

    protected override void OnPreRender(EventArgs e)
    {
        base.OnPreRender(e);
        idCount = 0;
        Panel1.Controls.Clear();
        if (comunicatro != null)
        {
            this.NewRankButton.Visible = false;
            this.ChooseRankButton.Visible = true;

            ButtonWS.Visible = false;
            ButtonBS.Visible = false;
            ButtonS.Visible = false;
            ButtonT.Visible = false;
            ButtonAg.Visible = false;
            ButtonInt.Visible = false;
            ButtonPer.Visible = false;
            ButtonWP.Visible = false;
            ButtonFel.Visible = false;


            ComunicationControl1.Comunicator = comunicatro.Current;
            //if (!comunicatro.MoveNext())
            //    comunicatro = null;
            return;
        }
        else
        {
            this.NewRankButton.Visible = true;
            this.ChooseRankButton.Visible = false;

            ButtonWS.Visible = true;
            ButtonBS.Visible = true;
            ButtonS.Visible = true;
            ButtonT.Visible = true;
            ButtonAg.Visible = true;
            ButtonInt.Visible = true;
            ButtonPer.Visible = true;
            ButtonWP.Visible = true;
            ButtonFel.Visible = true;

            ButtonWS.Enabled =Character !=null && Character.CanAccireWS;
            ButtonBS.Enabled =Character !=null && Character.CanAccireBS;
            ButtonS.Enabled = Character !=null &&Character.CanAccireS;
            ButtonT.Enabled = Character !=null &&Character.CanAccireT;
            ButtonAg.Enabled =Character !=null && Character.CanAccireAg;
            ButtonInt.Enabled=Character !=null && Character.CanAccireInt;
            ButtonPer.Enabled=Character !=null && Character.CanAccirePer;
            ButtonWP.Enabled =Character !=null && Character.CanAccireWp;
            ButtonFel.Enabled=Character !=null && Character.CanAccireFel;

         


            ComunicationControl1.Comunicator = null;


            CreateControl();
        }
    }

    private void CreateControl()
    {
        if (Character == null || !Character.IsFinished)
        {
            NewRankButton.Visible = false;
            ButtonWS.Visible = false;
            ButtonBS.Visible = false;
            ButtonS.Visible = false;
            ButtonT.Visible = false;
            ButtonAg.Visible = false;
            ButtonInt.Visible = false;
            ButtonPer.Visible = false;
            ButtonWP.Visible = false;
            ButtonFel.Visible = false;

            return;
        }
        else
            NewRankButton.Visible = true;

        ButtonWS.Text = "Raise WS (" + (Character.Career.WeaponSkillCost.Length > Character.TimesSkilledWS ? Character.Career.WeaponSkillCost[Character.TimesSkilledWS].ToString() : "N/A") + ")";
        ButtonBS.Text = "Raise BS (" + (Character.Career.BalisticSkillCost.Length > Character.TimesSkilledBS ? Character.Career.BalisticSkillCost[Character.TimesSkilledBS].ToString() : "N/A") + ")";
        ButtonS.Text = "Raise S (" + (Character.Career.StrengthCost.Length > Character.TimesSkilledS ? Character.Career.StrengthCost[Character.TimesSkilledS].ToString() : "N/A") + ")";
        ButtonT.Text = "Raise T (" + (Character.Career.ToughnessCost.Length > Character.TimesSkilledT ? Character.Career.ToughnessCost[Character.TimesSkilledT].ToString() : "N/A") + ")";
        ButtonAg.Text = "Raise Ag (" + (Character.Career.AgilityCost.Length > Character.TimesSkilledAg ? Character.Career.AgilityCost[Character.TimesSkilledAg].ToString() : "N/A") + ")";
        ButtonInt.Text = "Raise Int (" + (Character.Career.IntelligenceCost.Length > Character.TimesSkilledInt ? Character.Career.IntelligenceCost[Character.TimesSkilledInt].ToString() : "N/A") + ")";
        ButtonPer.Text = "Raise Per (" + (Character.Career.PerceptionCost.Length > Character.TimesSkilledPer ? Character.Career.PerceptionCost[Character.TimesSkilledPer].ToString() : "N/A") + ")";
        ButtonWP.Text = "Raise WP (" + (Character.Career.WillpowerCost.Length > Character.TimesSkilledWp ? Character.Career.WillpowerCost[Character.TimesSkilledWp].ToString() : "N/A") + ")";
        ButtonFel.Text = "Raise Fel (" + (Character.Career.FellowshipCost.Length > Character.TimesSkilledFel ? Character.Career.FellowshipCost[Character.TimesSkilledFel].ToString() : "N/A") + ")";

        this.NewRankButton.Enabled = Character.CanAccireNewRank;

        foreach (var rank in Character.Ranks)
        {
            var uc = (Controls_SpendExpOnRank)Page.LoadControl("~/Controls/SpendExpOnRank.ascx");//path to control
            
            uc.Rank = rank;
            uc.Accire = Action;
            uc.ID = (idCount++).ToString();
            uc.ClientIDMode = System.Web.UI.ClientIDMode.Static;
            Panel1.Controls.Add(uc);
        }
    }

    public void Action(HeresyManager.Rank.Buyable b, HeresyManager.Character character)
    {
        comunicatro = b.Accire(character);
        if (!comunicatro.MoveNext())
            comunicatro = null;
    }

    protected void NewRankButton_Click(object sender, EventArgs e)
    {
        comunicatro = Character.GetNewRank();
        if (!comunicatro.MoveNext())
            comunicatro = null;
    }
    protected void ChooseRankButton_Click(object sender, EventArgs e)
    {

        if (comunicatro != null && !comunicatro.MoveNext())
            comunicatro = null;
    }

    protected override object SaveViewState()
    {
        Session[guid + "comunicatro"] = comunicatro;
        return guid;
    }

    protected override void LoadViewState(object savedState)
    {
        guid = savedState as string;
        comunicatro = Session[guid + "comunicatro"] as IEnumerator<HeresyManager.Comunication.Comunicator>;

    }
    protected void ButtonWS_Click(object sender, EventArgs e)
    {
        Character.AccireWS();
    }
    protected void ButtonBS_Click(object sender, EventArgs e)
    {
        Character.AccireBS();
    }
    protected void ButtonS_Click(object sender, EventArgs e)
    {
        Character.AccireS();
    }
    protected void ButtonT_Click(object sender, EventArgs e)
    {
        Character.AccireT();
    }
    protected void ButtonAg_Click(object sender, EventArgs e)
    {
        Character.AccireAg();
    }
    protected void ButtonInt_Click(object sender, EventArgs e)
    {
        Character.AccireInt();
    }
    protected void ButtonPer_Click(object sender, EventArgs e)
    {
        Character.AccirePer();
    }
    protected void ButtonWP_Click(object sender, EventArgs e)
    {
        Character.AccireWp();
    }
    protected void ButtonFel_Click(object sender, EventArgs e)
    {
        Character.AccireFel();
    }
    protected void ResetButton_Click(object sender, EventArgs e)
    {
        UserData.Commit();
    }
    protected void SaveButton_Click(object sender, EventArgs e)
    {
        UserData.Roleback();
    }
}